<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\OrderAppraise as OrderAppraiseModel;
use think\Db;


/**
 * 订单评论接口
 */
class OrderAppraise extends Api
{
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 添加评论
     */
    public function add()
    {
        $param = $this->request->post();
        if(empty($param['name']) || empty($param['email']) || empty($param['content'])){
            $this->error('Name, email, and content cannot be empty!');
        }

        $model_temp = new OrderAppraiseModel();
        $model_temp->allowField(true)->save($param);

        $goods_score_all = Db::name('order_appraise')->where('goods_id',$param['goods_id'])->where('status','normal')->sum('goods_score');
        $pinglun_count = Db::name('order_appraise')->where('goods_id',$param['goods_id'])->where('status','normal')->count();
        $grade = round($goods_score_all / $pinglun_count,2);
        Db::name('litestore_goods')->where('goods_id',$param['goods_id'])->update(['grade'=>$grade]);

        $this->success();
    }

}
